#!/usr/bin/env/python2.5

## Modules for word conversion (for formatting in the prompts)

import string, re

def iscons(letter):
	"""Tests whether a letter is a consonant."""
	return (letter.isalpha() and (re.match(r"a|e|i|o|u", letter) == None))

def upper(word):
	## ~+##+~
	"""Puts a word in uppercase."""
	return word.capitalize()

def lower(word):
	## ~_##_~
	"""Puts a word in lowercase."""
	return word.lower()

def poss(word):
	## ~$##$~
	"""Converts a PRP into its PRP$."""
	word = word.lower()
	if (word == "it"):	return "its"
	if (word == "you"):	return "your"
	if (word == "y'all"):	return "y'all's"
	if ((word == "him") or (word == "he")):	return "his"
	if ((word == "her") or (word == "she")):	return "her"
	if ((word == "i") or (word == "me")):	return "my"
	if ((word == "we") or (word == "us")):	return "our"
	if ((word == "they") or (word == "them")):	return "their"

def gerund(word):
	## ~&##&~
	"""For converting a word from VB to VBG."""
	word = word.lower()

	## Rule 0:  Irregulars.
	if (word == "be"):	return "being"

	## Rule 1:  String the silent "e" first.
	if ((word[-1] == "e") and (iscons(word[-2]))):
		return (word[:-1] + "ing")

	## Rule 2:  Double the final consonant first.
	if (not iscons(word[-2]) and iscons(word[-1]) and (word[-3] != word[-2])):
		return (word + word[-1] + "ing")

	## Otherwise, just append "ing".
	return (word + "ing")

def base(word):
	## ~@##@~
	"""Convert a VBZ into a VB."""
	word = word.lower()

	## First, we resolve irregular verbs.
	if (word == "is"):	return "be"
	if (word == "has"):	return "have"
	
	## Next, words ending in "ies".
	if (word[-3:] == "ies"):	return (word[:-3] + "y")

	## Then, words ending in "es".
	if ((word[-2:] == "es") and (not iscons(word[-3]) or iscons(word[-4]))):
		return word[:-2]

	## All other verbs now.
	return (word[:-1])

def base2(word):
	## ~@##@~
	"""Convert a VBG into a VB."""
	word = word.lower()[:-3]

	## First, words with double consonants.
	if (iscons(word[-1]) and (word[-2] == word[-1])):	return word[:-1]
	
	## Next, words with a silent "e".
	if (iscons(word[-1]) and not iscons(word[-2]) and ((len(word) < 3) or (word[-3] != word[-2]))):	return (word + "e")

	## All other words now.
	return word

def passive(word):
	## ~|##|~
	"""A word is being converted here into its passive form from its VBZ form."""

	word = word.lower()

	## We will ignore the vast majority of irregular verbs at present, using only what we need.
	## Exceptions:
	if (word == "is"):	return "being"
	if (word == "becomes"):	return word
	if (word == "begins"):	return "begun"
	if (word == "bends"):	return "bent"
	if (word == "breaks"):	return "broken"
	if (word == "chooses"):	return "chosen"
	if (word == "does"):	return "done"
	if (word == "falls"):	return "fallen"
	if (word == "gets"):	return "gotten"
	if (word == "goes"):	return "gone"
	if (word == "gives"):	return "given"
	if (word == "was"):	return ""
	if (word == "throws"):	return "thrown"
	if (word == "catches"):	return "caught"

	## Verbs ending in "es":
	if (word[-2:] == "es"):	return (word[:-1] + "d")

	## Default:
	return (gerund(base(word))[:-3] + "ed")

def bool(word, adverb):
	"""Performs a minor Boolean change."""
	word = word.lower()
	adverb = adverb.lower()
	if (adverb == "not"):
		if (word == "or"):	return ("and", adverb)
		if (word == "and"):	return ("or", adverb)
	return (word, adverb)

## Test code:
if (__name__ == "__main__"):
	caplow = "I Jim Jennifer ALAN vince quincy brandon Maurice"
	pos = "James Dexter Moses he she it we they you y'all I"
	verbs = "do be judge bike fall miss shoot bat pat put"
	vbz = "is has was does tries throws catches assimilates flirts loves kisses taps"
	vbg = "being doing seeing crying aging seeming matriculating"
	lets = "abcde0-"
	print "LETTER:	CONSONANT?"
	for let in lets:
		print let + "\t", iscons(let)
	print "WORD:	UPPERCASE:	LOWERCASE:"
	for word in caplow.split():
		print word, "\t",
		print upper(word), "\t",
		print lower(word)
	print "WORD:	GENITIVE:"
	for word in pos.split():
		print word, "\t",
		print poss(word)
	print "WORD:	GERUND:"
	for word in verbs.split():
		print word, "\t",
		print gerund(word)
	print "WORD:	PASSIVE:	BASE:"
	for word in vbz.split():
		print word, "\t",
		print passive(word), "\t",
		print base(word)
	print "WORD:	BASE:"
	for word in vbg.split():
		print word, "\t",
		print base2(word)
	print "\nDone.\n"
